package com.mzhmxh.control;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mzhmxh.pojo.SmbmsBill;
import com.mzhmxh.pojo.SmbmsProvider;
import com.mzhmxh.pojo.SmbmsUser;
import com.mzhmxh.service.OrderService;
import com.mzhmxh.service.ProviderService;
import com.mzhmxh.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.json.JsonArray;
import javax.json.JsonValue;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author LiJinCheng
 * @Date 2020/7/11 3:24 下午
 * @Version 1.0
 * @package com.mzhmxh.control
 **/
@Controller
@RequestMapping("/bill")
public class OrderControl {
    @Autowired
    private OrderService orderService;
    @Autowired
    private ProviderService providerService;

    @RequestMapping("/list")
    public String list() {
        return "bill/list";
    }

    @RequestMapping("/add")
    public String add() {
        return "bill/add";
    }

    @RequestMapping("/bill_view")
    public String view() {
        return "bill/bii_view";
    }

    @RequestMapping("/bill_modify")
    public String modify() {
        return "bill/bill_modify";
    }

    @RequestMapping("/getBillList")
    @ResponseBody
    public String gtBillList(JsonMapper mapper, SmbmsBill bill, Integer curr, String goodsName, Integer provider, Integer payStaute) {
        try {
            if (curr == null) {
                curr = 1;
            }
            if (null != goodsName) {
                bill.setProductname(goodsName);
            }
            if (null != payStaute) {
                bill.setIspayment(payStaute);
            }
            if (null != provider) {
                bill.setProviderid(Long.valueOf(provider));
            }
            System.out.println("bill:" + bill);
            Page<Object> objects = PageHelper.startPage(curr, 5);
            List<Map<String, Object>> list = orderService.getList(bill);
            list.forEach(System.out::println);
            return mapper.writeValueAsString(list);
        } catch (Exception e) {
            e.printStackTrace();
            return "fail";
        }
    }

    @RequestMapping("/getBillPage")
    @ResponseBody
    public String gtBillPage(JsonMapper mapper, Integer curr, SmbmsBill bill, String goodsName, Integer provider, Integer payStaute) {
        try {
            if (curr == null) {
                curr = 1;
            }
            if (null != goodsName) {
                bill.setProductname(goodsName);
            }
            if (null != payStaute) {
                bill.setIspayment(payStaute);
            }
            if (null != provider) {
                bill.setProviderid(Long.valueOf(provider));
            }
            Page<Object> objects = PageHelper.startPage(curr, 5);
            List<Map<String, Object>> list = orderService.getList(bill);
            PageInfo pageInfo = new PageInfo(list);
            Map<String, Object> map = new HashMap<>();
            map.put("pagess", pageInfo.getPages());
            map.put("pageNum", pageInfo.getPageNum());
            map.put("totals", pageInfo.getTotal());
            return mapper.writeValueAsString(map);
        } catch (Exception e) {
            e.printStackTrace();
            return "fail";
        }
    }

    @RequestMapping("/addBill")
    @ResponseBody
    public String addBill( JsonMapper mapper, SmbmsBill bill, HttpSession session) {
        try {
            bill.setId((long) 0);
            bill.setCreatedby(((SmbmsUser)session.getAttribute("user")).getId());
            bill.setCreationdate(new SimpleDateFormat(DateUtil.FORMAT).parse(DateUtil.getDateTime()));
            System.out.println(bill);
            int insert = orderService.insert(bill);
            if (insert==1){
                return mapper.writeValueAsString("success");
            }else {
                return mapper.writeValueAsString("error");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @RequestMapping("/getBillCode")
    @ResponseBody
    public synchronized String billCode(JsonMapper mapper) {
        try {
            String[] dateTime = DateUtil.getDateTime().split(" ")[0].split("-");
            List<Map<String, Object>> list = orderService.getList(null);
            list.sort(Comparator.comparingInt(b -> Integer.parseInt(b.get("id").toString())));
            int id = Integer.parseInt(list.get(list.size() - 1).get("id").toString());
            StringBuffer sb = new StringBuffer();
            sb.append("BILL");
            sb.append(dateTime[0]);
            sb.append("_").append(id + 1);
            return mapper.writeValueAsString(sb.toString());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @RequestMapping("/viewBill")
    public String viewBill(Model model,Integer billid){
        SmbmsBill smbmsBill = orderService.selectByPrimaryKey(Long.valueOf(billid));
        model.addAttribute("bill",smbmsBill);
        SmbmsProvider smbmsProvider = providerService.selectByPrimaryKey(smbmsBill.getProviderid());
        model.addAttribute("pro",smbmsProvider);
        return "/bill/bii_view";
    }

    @RequestMapping("/delete")
    public String delete(){
        return null;
    }

    @RequestMapping("/modify")
    public String modify(Model model,Integer billid){
        SmbmsBill smbmsBill = orderService.selectByPrimaryKey(Long.valueOf(billid));
        model.addAttribute("bill",smbmsBill);
        List<SmbmsProvider> list = providerService.getList();
        model.addAttribute("pro",list);
        return "bill/bill_modify";
    }

    @RequestMapping("/doModify")
    @ResponseBody
    public String doModify(JsonMapper mapper,SmbmsBill bill){
        try {
            int i = orderService.updateByPrimaryKeySelective(bill);
            if (i==1){
                return mapper.writeValueAsString("success");
            }else {
                return mapper.writeValueAsString("fail");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
